﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
///</copyright>
using System;
using System.ComponentModel;
using Ferpect.ComponentModel.Design;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Ferpect.ComponentModel
{
    [DesignTimeVisible(false)]
    public sealed class AmbientProperties : Component, IGameComponent
    {
        private ContentManager content;
        private Texture2D blankTexture;

        public AmbientProperties()
        {
            this.ForeColor = Color.White;
            this.BackColor = Color.CornflowerBlue;
            this.HighlightColor = Color.Yellow;
        }

        [DisplayName("Font")]
        public string FontName { get; set; }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public SpriteFont Font { get; set; }

        [Editor(UITypeEditors.ColorEditor, UITypeEditors.UITypeEditor)]
        [DefaultValue(typeof(Color), "255,255,255,255")]
        public Color ForeColor { get; set; }

        [Editor(UITypeEditors.ColorEditor, UITypeEditors.UITypeEditor)]
        [DefaultValue(typeof(Color), "100,149,237,255")]
        public Color BackColor { get; set; }

        [Editor(UITypeEditors.ColorEditor, UITypeEditors.UITypeEditor)]
        [DefaultValue(typeof(Color), "255,255,0,255")]
        public Color HighlightColor { get; set; }

        /// <summary>
        /// A blank texture that can be used for colored backgrounds or overlays.
        /// </summary>
        public Texture2D BlankTexture { get { return this.blankTexture; } }

        #region IGameComponent Members

        public void Initialize()
        {
            ISite site = this.Site;
            if (site == null)
            {
                throw new InvalidOperationException("IComponent.Site is null. A site must be provided before calling IGameComponent.Initialize().");
            }

            this.content = new EmbeddedContentManager(site, this.GetType().Assembly, this.GetType().Namespace + ".EmbeddedContent." + Constants.PlatformNamespace);

            this.blankTexture = this.content.Load<Texture2D>("BlankTexture");

            if (String.IsNullOrEmpty(this.FontName))
            {
                this.Font = this.content.Load<SpriteFont>("DefaultFont");
            }
            else
            {
                IGameLoop gameState = (IGameLoop)site.GetService(typeof(IGameLoop));
                this.Font = gameState.Content.Load<SpriteFont>(this.FontName);
            }
        }

        #endregion

        #region IDisposable members

        protected override void Dispose(bool disposing)
        {
            if (this.content != null)
            {
                this.Font = null;
                this.content.Dispose();
                this.content = null;
            }
        }

        #endregion
    }
}
